import {
  set_CStyleData,
  set_CompsData,
  set_currComp,
  set_IsClickComponent,
  set_MenuShow,
  set_MenuStyle,
  set_MarklineFlag,
} from '../../actions/visualdrag';

/**
* initialState: 初始化可视化拖拽组件state
*/
const initialState = {
  canvasStyleData: { // 画布全局样式数据
    width: 1680,
    height: 945,
  },
  componentData: localStorage.componentData ? JSON.parse(localStorage.componentData) : [], // 画布组件数据
  curComponent: null, // 画布上当前选中的组件
  curComponentIndex: null,
  // 是否展示右键menu菜单
  menuShow: false,
  menuStyle: { // 右键menu菜单样式
    top: 0,
    left: 0,
  },
  // 点击画布时是否点中组件，主要用于取消选中组件用。
  // 如果没点中组件，并且在画布空白处弹起鼠标，则取消当前组件的选中状态
  isClickComponent: false,
  marklineFlag: { //标线信息
    index: 0,
    data: {
      isDownward: false,
      isRightward: false,
    }
  }
};
const Index = (state = initialState, action) => {
  switch (action.type) {
    case set_CStyleData:
      return { ...state, canvasStyleData: action.payload };
    case set_CompsData: {
      return { ...state, componentData: action.payload };
    }
    case set_currComp: { //设置当前选中组件数据，并改变componentData对应的值
      let data=state.componentData;
      data.map((mm,idx) => {
        if (mm.id == action.payload.index) {
          data[idx] = action.payload.component;
        }
      })
      return { ...state, componentData: data, curComponent: action.payload.component, curComponentIndex: action.payload.index }
    }
    case set_IsClickComponent: {
      return { ...state, isClickComponent: action.payload };
    }
    case set_MenuShow: {
      return { ...state, menuShow: action.payload };
    }
    case set_MenuStyle: {
      return { ...state, menuStyle: action.payload };
    }
    case set_MarklineFlag: {
      return { ...state, marklineFlag: action.payload };
    }
    default:
      return state;
  }
}
export default Index

